<?php


namespace App\Repositories;


use App\Exceptions\BasicException;
use App\Response\Response;
use App\Services\Tools\RedisService;
use Illuminate\Support\Facades\DB;

class WeekStarHistoryRepository
{
    public function getWeekRankInfo($weekId)
    {

        $where[]=['week_star_id','=',$weekId];
        $weekRanks = app('model_week_star_history')
                 ->with(['user_data:user_id,nickname'])
                ->with(['week_stars'=>function($query) {
                $query->select('id','gift_id_1','gift_id_2','weeks','period')->with(['gift_data1:id,name', 'gift_data2:id,name']);
            }])
            ->orderBy('rank', 'asc')->where($where)->paginate(3, ['*'], '', 1);
        if (empty($weekRanks))  throw new BasicException(98040);

        $weekTypeRanks=[];
        if($weekRanks){
            foreach ($weekRanks as $weekRank){
                if (empty($weekTypeRanks[$weekRank['type']])){
                    $weekTypeRanks[$weekRank['type']]=[$weekRank];
                }else{
                    array_push($weekTypeRanks[$weekRank['type']],$weekRank);
                }
            }
        }
        return $weekTypeRanks;
    }

    public function getWeekRankType($weeks,$type,$page,$perPage)
    {
        $where[]=['weeks','=',$weeks];
        $where[]=['deleted','=',0];
        $weekInfo = app('model_week_star')->where($where)->first();
        if (empty($weekInfo)) return[];

        $where1[]=['week_star_id','=',$weekInfo['id']];
        $where1[]=['type','=',$type];
        $weekRanks = app('model_week_star_history')->with(['user_data:user_id,nickname,head_picture,user_code'])
            ->with(['week_stars'=>function($query) {
                $query->select('id','gift_id_1','gift_id_2','weeks','period')->with(['gift_data1:id,name', 'gift_data2:id,name']);
            }])
            ->orderBy('rank', 'asc')->where($where1)->paginate($perPage, ['*'], '', $page);
        if ($weekRanks) return $weekRanks;
        throw new BasicException(98040);
    }

    public function userRankInfo($weeks,$type)
    {
        $historyModel = app("model_week_star_history");
        $userDataModel = app("model_user_data");
        $giftModel = app("model_week_star");
        $userRoomModel = app("model_user_room");

        $whereArr[]=['t1.type','=',$type];
        $whereArr[]=['t3.weeks','=',$weeks];
//        if ($nickname) {
//            if ($type==3) //房间榜
//            {
//                $whereArr[]=["t4.room_name", "like", "%". $nickname . "%"];
//            }else{
//                $whereArr[]=["t2.nickname", "like", "%". $nickname . "%"];
//            }
//        }

        $query = app('model_week_star_history')
            ->from($historyModel->getTable() . " as t1");
        if ($type==3) //房间榜
        {
            $query = $query ->select(
            "t4.room_id",
            "t1.type",
            "t1.rank",
            "t4.cover_picture",
            "t4.room_name",
            "t1.total",
            "t4.label_id"
            )->join(DB::raw($giftModel->getTable() . " as t3"), "t3.id", "=", "t1.week_star_id")
                ->join(DB::raw($userRoomModel->getTable() . " as t4"),"t4.id","=","t1.room_id")
                    ->join(DB::raw($userDataModel->getTable() . " as t2"),"t2.user_id","=","t1.user_id");
        }else{
            $query = $query ->select(
                "t1.rank",
                "t3.weeks",
                "t1.type",
                "t2.head_picture",
                "t2.nickname",
                "t1.total",
                "t2.user_code"
            )->join(DB::raw($giftModel->getTable() . " as t3"), "t3.id", "=", "t1.week_star_id")
                ->join(DB::raw($userDataModel->getTable() . " as t2"),"t2.user_id","=","t1.user_id");
        }
        $userInfoList = $query->where($whereArr)->get()->toArray();
        if ($userInfoList) return $userInfoList;
        throw new BasicException(98040);
    }

    /**
     * 获取本周周星榜富豪榜房间榜
     * @param $type
     * @param $nickname
     * @return array
     */
    public function thisWeekStar($type)
    {
        if ($type == 1){
            $key = 'weekstar:charm:' . date('oW');
        }
        if ($type == 2){
            $key = 'weekstar:rich:' . date('oW');
        }
        if ($type == 3){
            $key = 'weekstar:room:' . date('oW');
        }
        $list = app('redis')->zrevrange($key, 0, -1, "WITHSCORES");
        $data = [];
        $rank = 1;
        if ($type == 3){
            foreach ($list as $rid => $score){
                $temp = [];
                $room = app('model_user_room')->where('id',$rid)->first();
                $user = RedisService::getUserBaseInfo($room->user_id);
                $temp['rank'] = $rank;
                $temp['user_code'] = $user['user_code'];
                $temp['nickname'] = $room->room_name;
                $temp['head_picture'] = $room->cover_picture;
                $temp['total'] = $score;
                $temp['room_id'] = $room->room_id;
                $temp['room_label'] = app('model_room_label')->where('id', $room->label_id)->value('label_name');
                $rank++;
                $data[] = $temp;
            }
        }else{
            foreach ($list as $uid => $score){
                $temp = [];
                $user = RedisService::getUserBaseInfo($uid);
                $temp['rank'] = $rank;
                $temp['user_code'] = $user['user_code'];
                $temp['nickname'] = $user['nickname'];
                $temp['head_picture'] = $user['head_picture'];
                $temp['total'] = $score;
                $rank++;
                $data[] = $temp;
            }
        }
        return $data;
    }

    public function getHistoryRank($weekId,$nickname,$userCode,$type,$page,$perPage)
    {
        $whereArr[]=['t1.type','=',$type];
        $whereArr[]=['t1.rank','=',1];
        if ($nickname) {
            $whereArr[]=["t2.nickname", "like", $nickname . "%"];
        }
        if (!empty($userCode)){
            $whereArr[]=["t2.user_code", "=", $userCode];
        }
        if ($weekId > 0){
            $whereArr[]=["t1.week_star_id", "=", $weekId];
        }
        $historyModel = app("model_week_star_history");
        $userDataModel = app("model_user_data");
        $giftModel = app("model_week_star");
        $userRoomModel = app("model_user_room");

        $userInfoList= app('model_week_star_history')
            ->from($historyModel->getTable() . " as t1")
            ->select(
                "t1.week_star_id",
                "t1.room_id",
                "t3.weeks",
                "t3.period",
                "t2.head_picture",
                "t2.nickname",
                "t1.total",
                "t2.user_code"
            )->leftJoin(DB::raw($giftModel->getTable() . " as t3"), "t3.id", "=", "t1.week_star_id")
            ->leftJoin(DB::raw($userDataModel->getTable() . " as t2"),"t2.user_id","=","t1.user_id")
            ->where($whereArr)->orderBy('t3.id', 'desc')
            ->orderBy('total','desc')
            ->paginate($perPage, ['*'], '', $page);

        if ($type == 3){
            foreach ($userInfoList as $item){
                $room = $userRoomModel->where('id',$item->room_id)->first();
                $item->room_id = $room->room_id;
                $item->nickname = $room->room_name;
                $item->head_picture = $room->cover_picture;
                $item->room_label = app('model_room_label')->where('id',$room->label_id)->value('label_name');
            }
        }
        return $userInfoList;
    }

}
